// export function isPC() {
//   let flag = navigator.platform.match(/(Win|Mac)/i);
//   console.log("isPC", !!flag);
//   return !!flag;

// }

import { round } from "lodash-es";
function isIOS() {
  let u = navigator.userAgent;
  return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}
function isAndroid() {
  let u = navigator.userAgent;
  return u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
}

// 电话/姓名脱敏
export function telDesensitize(tel) {
  let regular = /(\d{3})\d*(\d{4})/;
  return tel.replace(regular, "$1****$2");
}

export function nameDesensitize(name) {
  let userName = "";
  if (name.length == 2) {
    userName = name.substring(0, 1) + "*"; //截取name 字符串截取第一个字符，
  } else if (name.length == 3) {
    userName = name.substring(0, 1) + "*" + name.substring(2, 3); //截取第一个和第三个字符
  } else if (name.length > 3) {
    userName =
      name.substring(0, 1) + "*" + "*" + name.substring(3, name.length); //截取第一个和大于第4个字符
  }
  return userName;
}

export function judgeObjIsNull(obj) {
  return Object.keys(obj).length === 0;
}

// 微信分享配置函数
export const wxShareConfig = () => {
  return {
    link: "https://zxcxweixin.cxbtv.cn/2023/yayunzhishi/#/", //根据实际修改
    h5_title: "超级宝宝投票", //根据实际修改
    img_url:
      "https://image.cxbtv.cn/profile/E7BBBCE59088E7AEA1E79086E5B9B3E58FB02DE696BDE696B9E6988E/2023/06/15/8bb8da90f6982a3f47df5281acfe92b6.jpg", //根据实际修改
    desc: "长兴传媒集团荣誉出品",
  };
};

export const removeTel = () => {
  localStorage.removeItem("userInfo");
};

/**
 * @description: 判断横屏竖屏
 * @return {Boolean} true 横屏 false 竖屏
 */
export const judgeScreeIsVorH = function () {
  let screenWidth = window.screen.width,
    screenHeight = window.screen.height;
  if (screenWidth > screenHeight) {
    return true;
  } else {
    return false;
  }
};

/**
 * @description: 防抖函数
 * @param {Function} fun 需要防抖的函数
 * @param {number} delay 延迟时间
 * @param {boolean} immediate 是否立马执行
 * @return {Function} 返回函数
 */
export const debounce = (func, delay = 300, immediate = false) => {
  let timer = null;
  let callNow = immediate;
  return function () {
    const context = this;
    const args = arguments;
    clearTimeout(timer);
    if (callNow && !timer) {
      func.apply(context, args);
      callNow = false;
    }
    timer = setTimeout(() => {
      timer = null;
      if (!callNow) {
        func.apply(context, args);
      }
    }, delay);
  };
};
/**
 * @brief:
 * @param {*Function} func 需要节流的函数
 * @param {*number} interval 间隔
 * @param {*boolean} immediate 是否立即执行
 * @return {*}
 */
export const throttle = (func, interval, immediate) => {
  let lastTime = 0;
  let timer = null;
  let callNow = immediate;
  return function () {
    const context = this;
    const args = arguments;
    const currentTime = Date.now();
    if (callNow) {
      if (!timer) {
        func.apply(context, args);
        lastTime = currentTime;
        timer = setTimeout(() => {
          timer = null;
        }, interval);
        callNow = false;
      }
    } else {
      if (currentTime - lastTime > interval) {
        func.apply(context, args);
        lastTime = currentTime;
      }
    }
  };
};

//判断微信环境
export const isInWinXin = () => {
  let ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
};

//判断掌心长兴App环境
export const isZxcxApp = () => {
  return navigator.userAgent.match(/(zxcxapp)/i);
};

//时间格式化

export const formatTime = (dateObj, formatStr = "yyyy-MM-dd") => {
  if (!dateObj) return "";
  const date = new Date(dateObj);
  if (isNaN(date.getTime())) return "Invalid Date";
  const padZero = (num) => num.toString().padStart(2, "0");
  const parts = {
    "y+": date.getFullYear(), // 年
    "M+": date.getMonth() + 1, // 月
    "d+": date.getDate(), // 日
    "h+": date.getHours() % 12 || 12, // 12小时制
    "H+": date.getHours(), // 24小时制[5](@ref)
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? "AM" : "PM", // 上午/下午[5](@ref)
    W: ["日", "一", "二", "三", "四", "五", "六"][date.getDay()], // 星期[8](@ref)
  };
  let formatted = formatStr;
  for (const [key, value] of Object.entries(parts)) {
    const regex = new RegExp(`(${key})`, "g");
    formatted = formatted.replace(regex, (match) => {
      // 年份特殊处理（适配2位/4位）
      if (key === "y+") {
        return match.length === 2 ? value.toString().slice(-2) : value;
      }
      // 数字补零逻辑[4](@ref)
      return typeof value === "number" && match.length > 1
        ? padZero(value)
        : value;
    });
  }
  return formatted;
};

// 理想宽度，设计稿的宽度
const idealWidth = 1260;

// 表示伸缩视图的最大宽度
const maxWidth = 600;

export function vw(n) {
  if (n === 0) return n;

  const vwN = round((n * 100) / idealWidth, 3);
  const maxN = round((n * maxWidth) / idealWidth, 3);
  const cssF = n > 0 ? "min" : "max";
  return `${cssF}(${vwN}vw, ${maxN}px)`;
}
